/**
 * 参数配置表管理初始化
 */
var Drug = {
    id: "DrugTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

var DrugCategory = {
    id: "DrugCategoryTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Drug.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '代码', field: 'dicCode', align: 'center', valign: 'middle', sortable: true},
        {title: '名称', field: 'dicDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '取值内容', field: 'getList', align: 'center', valign: 'middle', sortable: true},
        {title: '备注说明', field: 'resume', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:Drug.edit(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
    		_html += '<a href="javascript:Drug.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
    		return _html;
        }}];
};

DrugCategory.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '来源类型', field: 'sourceDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '字典代码', field: 'dicCatCode', align: 'center', valign: 'middle', sortable: true},
        {title: '字典名称', field: 'dicCatDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '输出内容', field: 'sourceFieldDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '备注说明', field: 'resume', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:DrugCategory.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:DrugCategory.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
}

/**
 * 检查是否选中
 */
Drug.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Drug.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增药品名称
 */
Drug.add = function () {
    var index = Sys.open('新增药品名称',Sys.backendPath + '/sds/sdsstanddicsub/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看药品名称详情
 */
Drug.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('药品名称详情',Sys.backendPath + '/sds/sdsstanddicsub/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除药品名称
 */
Drug.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicsub/delete", function (data) {
	            Sys.success("删除成功!");
                Drug.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsStandDicsubId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除药品名称?",operation);
    }
};

/**
 * 查询参数配置表列表
 */
Drug.search = function () {
    var queryData = {};
    queryData['categoryId'] = Drug.categoryId==0?"":Drug.categoryId;
    queryData['dicCode'] = $("#dicCode").val();
    queryData['dicDesc'] = $("#dicDesc").val();
    Drug.table.refresh({query: queryData});
};

Drug.onClickConfig = function (e, treeId, treeNode) {
    Drug.categoryId = treeNode.id;
    Drug.categoryName = treeNode.name;
    Drug.search();
};


/**
 * 点击添加药品分类
 */
Drug.openAddCategory = function () {
	this.layerIndex = Sys.open('新增分类',Sys.backendPath + '/sds/sdsstanddic/to_add',['600px', '400px']);
};

/**
 * 打开查看药品分类详情
 */
Drug.openCategoryDetail = function () {
    if (Drug.categoryId) {
    	this.layerIndex = Sys.open('修改分类',Sys.backendPath + '/sds/sdsstanddic/to_update/' + Drug.categoryId,['600px', '400px']);
    }else{
        Sys.info("请先选中某一分类！");
    }
};

/**
 * 删除药品分类
 */
Drug.delCategory = function () {
    if (Drug.categoryId) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddic/delete", function (data) {
	            Sys.success("删除成功!");
                Drug.ztree.init();
                Drug.categoryId=null;
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsStandDicId",Drug.categoryId);
	        ajax.start();
	    };
	    Sys.confirm("是否删除药品分类?",operation);
    }else{
        Sys.info("请先选中某一分类！");
    }
};
$(function () {
    var defaultColunms = Drug.initColumn();
    var table = new BSTable(Drug.id, Sys.backendPath + "/sds/sdsstanddrug/list", defaultColunms);
    table.setPaginationType("server");
    Drug.table = table.init();

    var defaultCategoryColunms = DrugCategory.initColumn();
    var tableCategory = new BSTable(DrugCategory.id, Sys.backendPath + "/sds/sdsstanddic/list", defaultCategoryColunms);
    tableCategory.setPaginationType("server");
    DrugCategory.table = tableCategory.init();

});
